import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class TCPServer extends ServerSocket implements Runnable
{
	private static int PORT = 32486;
	private boolean runListen = false;	
	private Map<String,Socket> sockets = new ConcurrentHashMap <String,Socket>() ;

	public TCPServer() throws IOException 
	{
		this(PORT);
	}

	public TCPServer(int port) throws IOException 
	{
		super(port);
	}

	public Socket getConnect() throws IOException
	{
		return this.accept();
	}

	public void startListen()
	{
		runListen = true;
		Thread t = new Thread(this);
		t.start();

	}

	public void stopListen()
	{
		runListen = false;
	}

	public void run() 
	{
		while(runListen)
		{
			Socket temp = null;
			try 
			{
				temp = getConnect();
				synchronized (sockets) 
				{
					sockets.put(temp.getInetAddress().getHostAddress(), temp);
				}
			} 
			catch (IOException e) 
			{
				e.printStackTrace();
			}
		}
	}



}
